<?php
// 预约历史信息
// header("content-type:text/html;charset=UTF-8");
include_once("conn.php");
include_once("u_getLoginInfo.php");

if (!isset($_GET['bDate']) && !isset($_GET['eDate'])) {
    $select_info = "SELECT * FROM order_history_infos
    WHERE user_ID = '" . $userID . "' ORDER BY order_Date DESC";
} else {
    $b_date = $_GET['bDate'];
    $e_date = $_GET['eDate'];
    if ($b_date == "") {
        $b_date = "1970-01-01";
    }

    if ($e_date == "") {
        date_default_timezone_set('Asia/Shanghai');
        $e_date = date("Y-m-d");
    }

    $select_info = "SELECT * FROM order_history_infos 
    WHERE order_Date BETWEEN '" . $b_date . "' AND '" . $e_date . "' 
    AND user_ID = '" . $userID . "' ORDER BY order_Date DESC";
}

$result_select = mysqli_query($connect_DB, $select_info);
if ($result_select) {
    if (mysqli_num_rows($result_select) > 0) {
        while ($row = mysqli_fetch_object($result_select)) {
            //分别查询对应的医生和宠物信息
            $select_petName = "SELECT pet_Name FROM pet_infos WHERE pet_ID = '" . $row->pet_ID . "'";
            $result_petName = mysqli_query($connect_DB, $select_petName);
            $row_petName = mysqli_fetch_object($result_petName)->pet_Name;

            $select_docName = "SELECT doc_Name FROM doc_infos WHERE doc_ID = '" . $row->doc_ID . "'";
            $result_docName = mysqli_query($connect_DB, $select_docName);
            $row_docName = mysqli_fetch_object($result_docName)->doc_Name;

            if ($row->orderState == 1) {
                $state = "已完成";
            } elseif ($row->orderState == 0) {
                $state = "取消审核中";
            } elseif ($row->orderState == -1) {
                $state = "已被取消";
            }

            echo "
            <div class='history-list-div'>
                <div class='list-div'>" . $row_petName . "</div>
                <div class='list-div'>" . $row->order_Type . "</div>
                <div class='list-div'>" . $row_docName . "</div>
                <div class='list-div-l'>" . $row->order_Remark . "</div>
                <div class='list-div'>" . $row->sub_Date . "</div>
                <div class='list-div'>" . $row->order_Date . "</div>
                <div class='list-div'>" . $state . "</div>
            </div>
            ";
        }
    } else {
        if (!isset($_GET['bDate']) && !isset($_GET['eDate'])) {
            echo "
            <div class='history-list-div'>
                <div class='list-div'>暂时没有您的预约历史信息</div>
            </div>
            ";
        } else {
            echo "
            <div class='history-list-div'>
                <div class='list-div'>该时间段没有您的预约历史信息</div>
            </div>
            ";
        }
    }
} else {
    echo "
    <div class='history-list-div'>
        <div class='list-div'>查询出错：" . $select_info . "</div>
    </div>
    ";
}

$connect_DB->close();
?>